package keyboard;

public class Position {
	public float r, c;
	
	public Position() {
		r = c = -1f;
	}
	
	public Position(float _r, float _c) {
		r = _r;
		c = _c;
	}
	
	@Override
	public String toString() {
		return r + ", " + c;
	}
	
	public static Position parsePosition(String string) {
		string = string.substring(1, string.length() - 1);
		String[] tokens = string.split("; ");
		return new Position(Float.parseFloat(tokens[0]), Float.parseFloat(tokens[1]));
	}
	
	public static float ManhattanDist(Position A, Position B) {
		return Math.abs(A.r - B.r) + Math.abs(A.c - B.c);
	}
	
	public static float EuclidDist(Position A, Position B) {
		return (float)Math.sqrt(Math.pow(A.r - B.r, 2f) + Math.pow(A.c - B.c, 2f));
	}
	
	public static float Dot(Position A, Position B) {
		return A.r * B.r + A.c * B.c;
	}
	
	public static Position GetMinPosition(Position A, Position B) {
		return new Position(Math.min(A.r, B.r), Math.min(A.c, B.c));
	}
	
	public static Position GetMaxPosition(Position A, Position B) {
		return new Position(Math.max(A.r, B.r), Math.max(A.c, B.c));
	}
	
	public static Position GetVectorFromAToB(Position A, Position B) {
		return new Position(B.r - A.r, B.c - A.c);
	}
	
	public static Position getPressPosition(Position startPosition, Position endPosition, Position size) {
		Position dPosition = new Position(Math.abs(endPosition.r - startPosition.r), Math.abs(endPosition.c - startPosition.c));
		Position halfSize = new Position((size.r - 1f) * 0.5f, (size.c - 1f) * 0.5f);
		if (dPosition.r <= halfSize.r && dPosition.c <= halfSize.c) {
			return startPosition;
		}
		float endR = startPosition.r > endPosition.c ? endPosition.r + halfSize.r : endPosition.r - halfSize.r;
		float endC = startPosition.c > endPosition.c ? endPosition.c + halfSize.c : endPosition.c - halfSize.c;
		if (dPosition.r <= halfSize.r && dPosition.c > halfSize.c)
			return new Position(startPosition.r, endC);
		if (dPosition.r > halfSize.r && dPosition.c <= halfSize.c)
			return new Position(endR, startPosition.c);
		return new Position(endR, endC);
	}
}
